package com.sebiotec.ambassador.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sebiotec.ambassador.R;
import com.sebiotec.ambassador.R2;
import com.sebiotec.ambassador.adapt.JobAdapt;
import com.sebiotec.ambassador.model.event.JobEvents;
import com.sebiotec.ambassador.model.response.JobListResponse;
import com.sebiotec.ambassador.viewmodel.JobViewModel;
import com.sebiotec.basecommon.utils.base.BaseFragment;
import com.sebiotec.basecommon.utils.tools.SoftKeyBoardListener;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xuexiang.xui.widget.statelayout.MultipleStatusView;
import com.xuexiang.xui.widget.toast.XToast;
import com.xw.repo.XEditText;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AmJobFragment extends BaseFragment {

    @BindView(R2.id.iv_searchss)
    ImageView ivSearchss;
    @BindView(R2.id.ed_history_seach)
    XEditText edHistorySeach;
    @BindView(R2.id.iv_search)
    ImageView ivSearch;
    @BindView(R2.id.multiple_status_view)
    MultipleStatusView multipleStatusView;
    @BindView(R2.id.rl_pending)
    RelativeLayout rlPending;
    @BindView(R2.id.recy_message)
    RecyclerView recyMessage;
    @BindView(R2.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R2.id.rl_shape)
    RelativeLayout rlShape;


    private String mSearch;
    private boolean isShow = false;
    private JobViewModel jobViewModel;
    private JobAdapt jobAdapt;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.ambassador_fragment_job, container, false);
        ButterKnife.bind(this, view);
        jobViewModel = ViewModelProviders.of(this).get(JobViewModel.class);
        InitUI();
        return view;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void InitUI() {
        EventBus.getDefault().register(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyMessage.setLayoutManager(linearLayoutManager);
    }

    @Override
    public void initData() {
        multipleStatusView.showLoading();
        getFristData();

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mSearch = "";
                edHistorySeach.setTextEx(" ");
                jobViewModel.getJobData("", mSearch, 0, 20, new JobViewModel.MyJobDataCallBack<Object>() {
                    @Override
                    public void onDataSuccessCallBack(Object o) {
                        multipleStatusView.showContent();
                        JobListResponse response = (JobListResponse) o;
                        if (response.getRows().size() == 0 || response.getRows() == null) {
                            multipleStatusView.showEmpty();
                        } else {
                            jobAdapt = new JobAdapt(R.layout.item_ambassador, response.getRows());
                            recyMessage.setAdapter(jobAdapt);
                        }
                        refreshLayout.finishRefresh();
                    }

                    @Override
                    public void onDataFailCallBack(Object o) {
                        multipleStatusView.showError();
                        refreshLayout.finishRefresh(false);
                    }
                });
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(final RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore();//传入false表示加载失败
                refreshLayout.finishLoadMoreWithNoMoreData();


            }
        });

        edHistorySeach.setOnXFocusChangeListener(new XEditText.OnXFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    rlShape.setBackground(getActivity().getDrawable(R.drawable.ll_half_shape_ed));
                    ivSearch.setImageResource(R.drawable.seach_white);
                } else {
                    // 此处为失去焦点时的处理内容
                    rlShape.setBackground(getActivity().getDrawable(R.drawable.ll_half_shape));
                    ivSearch.setImageResource(R.drawable.search);
                }
            }
        });
        edHistorySeach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edHistorySeach.setFocusable(true);
                edHistorySeach.setFocusableInTouchMode(true);
                edHistorySeach.requestFocus();
            }
        });
        SoftKeyBoardListener.setListener(getActivity(), new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {

            }

            @Override
            public void keyBoardHide(int height) {
                edHistorySeach.setFocusable(false);
            }
        });
        multipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                multipleStatusView.showLoading();
                initData();
            }
        });

    }

    private void getFristData() {
        jobViewModel.getJobData("", mSearch, 0, 20, new JobViewModel.MyJobDataCallBack<Object>() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                multipleStatusView.showContent();
                JobListResponse response = (JobListResponse) o;
                if (response.getRows().size() == 0 || response.getRows() == null) {
                    multipleStatusView.showEmpty();
                } else {
                    jobAdapt = new JobAdapt(R.layout.item_ambassador, response.getRows());
                    recyMessage.setAdapter(jobAdapt);
                }
            }

            @Override
            public void onDataFailCallBack(Object o) {
                multipleStatusView.showError();
            }
        });
    }

    @OnClick({R2.id.iv_searchss, R2.id.iv_search})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.iv_searchss) {
            if (isShow) {
                rlPending.setVisibility(View.GONE);
                isShow = false;
            } else {
                rlPending.setVisibility(View.VISIBLE);
                isShow = true;
            }
        } else if (id == R.id.iv_search) {
            mSearch=edHistorySeach.getTextEx().toString();
            multipleStatusView.showLoading();
            getFristData();

        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventaa(JobEvents event) {
        if (event.getType() == 1) {
            jobAdapt.remove(event.getPos());
            multipleStatusView.showLoading();
            getFristData();
        }else if (event.getType() == 2){
            XToast.error(getContext(),"aa").show();
        }

    }

}
